<?php

namespace Kunfeng\Dify\Model\Common;

/**
 * 使用量统计模型
 */
class Usage
{
    private $totalTokens;
    private $totalPrice;
    private $promptTokens;
    private $completionTokens;

    public function __construct(
        int $totalTokens,
        float $totalPrice,
        int $promptTokens = 0,
        int $completionTokens = 0
    ) {
        $this->totalTokens = $totalTokens;
        $this->totalPrice = $totalPrice;
        $this->promptTokens = $promptTokens;
        $this->completionTokens = $completionTokens;
    }

    /**
     * 从数组创建使用量
     */
    public static function fromArray(array $data): self
    {
        return new self(
            $data['total_tokens'] ?? 0,
            $data['total_price'] ?? 0.0,
            $data['prompt_tokens'] ?? 0,
            $data['completion_tokens'] ?? 0
        );
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'total_tokens' => $this->totalTokens,
            'total_price' => $this->totalPrice,
            'prompt_tokens' => $this->promptTokens,
            'completion_tokens' => $this->completionTokens,
        ];
    }

    public function getTotalTokens(): int
    {
        return $this->totalTokens;
    }

    public function getTotalPrice(): float
    {
        return $this->totalPrice;
    }

    public function getPromptTokens(): int
    {
        return $this->promptTokens;
    }

    public function getCompletionTokens(): int
    {
        return $this->completionTokens;
    }
}